//
// Created by liaohailong on 2025/2/28.
//

#include <string>
#include <vector>
#include <unordered_map>
#include <set>

using namespace std;

// 2353 设计食物评分系统
class FoodRatings
{
public:
    FoodRatings(vector<string> &foods, vector<string> &cuisines, vector<int> &ratings) :
            foodMap(), cuisineMap()
    {
        size_t N = foods.size();

        for (int i = 0; i < N; ++i)
        {
            string &food = foods[i];
            string &cuisine = cuisines[i];
            int rating = ratings[i];
            foodMap[food] = {rating, cuisine};

            // set原本是升序排序，这里用负分数实现倒叙
            // 烹饪方式 -> SET(负分数，食物名称)
            cuisineMap[cuisine].insert({-rating, food});
        }
    }


    void changeRating(const string &food, int newRating)
    {
        // 分数，烹饪方式
        pair<int, string> &foodInfo = foodMap[food];
        string &cuisine = foodInfo.second;
        int rating = foodInfo.first;
        cuisineMap[cuisine].erase({-rating, food});
        cuisineMap[cuisine].insert({-newRating, food});
        foodInfo.first = newRating;
    }

    string highestRated(const string &cuisine)
    {
        // 每次取第一个就是最大值
        return cuisineMap[cuisine].begin()->second;
    }

private:
    unordered_map<string, pair<int, string>> foodMap;
    unordered_map<string, set<pair<int, string>>> cuisineMap;
};

/**
 * Your FoodRatings object will be instantiated and called as such:
 * FoodRatings* obj = new FoodRatings(foods, cuisines, ratings);
 * obj->changeRating(food,newRating);
 * string param_2 = obj->highestRated(cuisine);
 */
